package com.swordget.service.impl;

import com.swordget.dao.DataStorageDao;
import com.swordget.entity.DataSource;
import com.swordget.entity.vo.SimplePageRequestVo;
import com.swordget.entity.vo.SimplePageResponseVo;
import com.swordget.exception.VerifyFailException;
import com.swordget.service.DataStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: DataStorageServiceimp
 * @Description: 数据仓库管理服务实现
 * @Author: 陈根
 * @Date: 2020/7/2 16:04
 * @Version
 **/
@Service
public class DataStorageServiceimp implements DataStorageService {

    @Autowired
    private DataStorageDao dataStorageDao;



    //新增数据源方法
    @Override
    public boolean addDataSource(DataSource dataSource) {
       dataSource.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
       dataSource.setCreateTime(new SimpleDateFormat("HH:mm:ss").format(new Date()));

        return dataStorageDao.addDataSource(dataSource);
    }

    //查询数据源
    @Override
    public SimplePageResponseVo getDataSource(SimplePageRequestVo pageVo) {
        int rows = pageVo.getRows();
        int page = pageVo.getPage();

        List<DataSource> data=dataStorageDao.findDataSoucreByPage(rows,page);
        int count=dataStorageDao.findTotal();
       return SimplePageResponseVo.builder()
                .total(count)
                .rows(data)
                .build();
    }


    //删除数据源
    @Override
    public boolean delDataStorage(String dataid) {

        //drop 首先判断是否在存在这个数据源
       boolean isExists=dataStorageDao.isExists(dataid);
       if (!isExists){
          throw new VerifyFailException("该数据源不存在或不是通过系统创建!");
      }
      boolean isDrop=dataStorageDao.dropData(dataid);

        return isDrop;
    }

    //修改数据源
    @Override
    public boolean editDataSource(DataSource dataSource) {
        dataSource.setCreateDate(new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        dataSource.setCreateTime(new SimpleDateFormat("HH:mm:ss").format(new Date()));

        return dataStorageDao.editDataSource(dataSource);
    }
}
